<?php

namespace Home\Controller;
use Common\Controller\BaseController;

/**
 * Class EquRepairRecordsCommentController
 * @package Home\Controller
 * 
 */
class EquRepairRecordsCommentController extends BaseController {

   public function index(){
        $repair_record_no = I('get.repair_record_no','','htmlspecialchars');
        $number = I('get.number','','htmlspecialchars');

        $equRecordsModel = D('EquRepairRecords');

        $where = " equ_repair_records.del=1 ";
        
        if(!empty($repair_record_no)){
        	$where  .= " and locate('{$repair_record_no}',equ_repair_records.repair_record_no) ";
        }

        if(!empty($number)){
        	$where  .= " and locate('{$number}',equipment.number) ";
        }

        $field = "equ_repair_records.*,user.name as uname,user.nickname,equipment.number as equnumber,equipment.name as equname,comment.score";
        $list = $equRecordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=equ_repair_records.equipment_id','LEFT')
            ->join('comment on equ_repair_records.comment_id=comment.comment_id','LEFT')
            ->join('user on equ_repair_records.user_id=user.user_id','LEFT')
            ->where($where)
            ->order('equ_repair_records_id DESC')
            ->select();
         // echo $equRecordsModel->_sql();
         // dump($list);exit;
        $this->assign('list',$list);
        $this->assign('repair_record_no',$repair_record_no);
        $this->assign('number',$number);

        $this->display('index');
    }

    public function add(){
        
        $equ_id = I('get.equid',0,'int');
        $equRecordsModel    = D('EquRepairRecords');

        $where = " equ_repair_records_id={$equ_id} ";

        if(0!=$this->company_r_id){
            $where .= " and company_id=".$this->company_r_id." ";
        }
       
        $equ_repair_records = $equRecordsModel->where($where)->limit(1)->select();

        if(empty($equ_repair_records[0])){
            $this->error('无权限');
        }

        $question_list = D('CommentQuestion')->getQuestionList();
        
        $this->assign('question_list',$question_list);
        $this->assign('equ_repair_records',$equ_repair_records[0]);
        $this->assign('level',C('level'));
        $this->display('add');
    }

    public function edit(){

        $equ_id = I('get.equid',0,'int');
        $equRecordsModel    = D('EquRepairRecords');

        $where = " equ_repair_records_id={$equ_id} ";

        if(0!=$this->company_r_id){
            $where .= " and company_id=".$this->company_r_id." ";
        }
       
        $equ_repair_records = $equRecordsModel->where($where)->limit(1)->select();

        if(empty($equ_repair_records[0])){
            $this->error('无权限');
        }
        $question_list = D('CommentQuestion')->getQuestionList();

        $comment_id = I('get.commentid',0,'int');
        $commentModel = D('Comment');
        $comment = $commentModel->where('comment_id='.$comment_id)->select();

        if(empty($comment)){
            $this->error('评分不存在');
        }
        $extern = json_decode($comment[0]['ext'],true);
        
        foreach ($extern['question_list'] as $key => $value) {
            $exkey = key($value);
            $comment[0]['extern'][$exkey] = $value[$exkey];
        }

        // dump($comment[0]);
        // exit;
        $this->assign('comment',$comment[0]);
        
        $this->assign('question_list',$question_list);
        $this->assign('equ_repair_records',$equ_repair_records[0]);
        $this->assign('level',C('level'));

        $this->display('edit');
    }

    public function save(){

        if(IS_POST){
            try{

                $commentModel = D('Comment');
                $post      = I('post.');
                $res = $commentModel->saveComment($post);
                if($res){
                    echo "<script>alert('操作成功');location.href='".U('Home/EquRepairRecordsComment/index')."' </script>";
                }else{
                    echo "<script>alert('操作失败');location.href='".U('Home/EquRepairRecordsComment/index')."' </script>";
                }

            }catch (\Exception $e){
                $error_str = $e->getMessage()." on line ".$e->getLine();
                echo "<script>alert('".$error_str."');location.href='".U('Home/EquRepairRecordsComment/index')."' </script>";
            }
        }else{
            echo "<script>alert('请求不允许');location.href='".U('Home/EquRepairRecordsComment/index')."' </script>";
        }

        exit;
    }
    public function delete(){

        
        $equ_id    = I('get.equid');
        $equModel  = D('EquRepairRecords');

        $equipment = $equModel->where('equ_repair_records_id='.intval($equ_id).' and del=1')->limit(1)->select();
	
        if(!empty($equipment)){
            try{
                $info = array('equ_repair_records_id'=>intval($equ_id),'del'=>2);
                $res = $equModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '维保纪录不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $equ_id    = I('get.equid');
        $equRecordsModel  = D('EquRepairRecords');
        
        $field = "equ_repair_records.*,user.name as uname,user.nickname,equipment.number as equnumber,equipment.name as equname,comment.score";
        $equ_repair_records = $equRecordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=equ_repair_records.equipment_id','LEFT')
            ->join('comment on equ_repair_records.comment_id=comment.comment_id','LEFT')
            ->join('user on equ_repair_records.user_id=user.user_id','LEFT')
            ->where("equ_repair_records_id={$equ_id} and equ_repair_records.del=1")
            ->limit(1)
            ->select();

        if(empty($equ_repair_records)){
            $this->error('维保纪录不存在');
        }

        $this->assign('equ_repair_records',$equ_repair_records[0]);
        $this->display('view');
    }

}